package com.example.leetcode.trainingcamp.week2.practice;

public class Offer58 {

    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        ListNode pre = new ListNode();
        ListNode now = pre;
        while (l1 != null && l2 != null){
            int l1Value = l1.val;
            int l2Value = l2.val;
            if (l1Value <= l2Value){
                now.next = l1;
                l1 = l1.next;
            }else {
                now.next = l2;
                l2 = l2.next;
            }
            now = now.next;
        }
        if (l1 != null){
            now.next = l1;
        }
        if (l2 != null){
            now.next = l2;
        }
        return pre.next;
    }



    public ListNode mergeTwoLists2(ListNode l1, ListNode l2){
        if (l1 == null || l2 == null){
            return l1 != null ? l1:l2;
        }
        if (l1.val <l2.val){
            l1.next = mergeTwoLists2(l1.next,l2);
            return l1;
        }else {
            l2.next = mergeTwoLists2(l1,l2.next);
            return l2;
        }
    }
}
